<!--
 * @Author: MarsLay
 * @Date: 2021-12-23 09:39:56
 * @LastEditTime: 2024-01-05 15:54:37
 * @LastEditors: MarsLay 331412466@qq.com
 * @Description: 自定义头部组件，可动态配置右侧按钮功能和title字段/返回路径
 * @FilePath: \cloud-space\components\header\header.vue
-->
<template>
  <view :class="{ header: !nav }"
    :style="{ position: nav ? 'relative' : '', height: nav ? nav.navHeight + 'px' : '', 'background': bgcolor, 'color': color }">
    <view v-if="nav" class="inner"
      :style="{ height: nav.menuHeight + 'px', minHeight: nav.menuHeight + 'px', lineHeight: nav.menuHeight + 'px', bottom: nav.menuBottom + 'px' }">
      <view v-if="!noBack" class="back" @click="goBack">
        <image class="back-icon" mode="aspectFit" src="../../static/img/icon/back-b.png"></image>
      </view>
      <view class="all-title">{{ title }}</view>
      <view class="list" v-if="buttons.length > 0">
        <view @click="handler(v.key)" class="single"
          :style="{ width: `${v.w}${nav ? 'rpx' : 'upx'}`, height: `${v.h}${nav ? 'rpx' : 'upx'}` }"
          v-for="(v, i) in buttons" :key="i">
          <image model="aspectFill" :src="v.path"></image>
        </view>
      </view>
    </view>
    <block v-else>
      <view v-if="!noBack" class="back" @click="goBack">
        <image class="back-icon" mode="aspectFit" src="../../static/img/icon/back-b.png"></image>
      </view>
      <view class="title">{{ title }}</view>
      <view class="list" v-if="buttons.length > 0">
        <view @click="handler(v.key)" class="single"
          :style="{ width: `${v.w}${nav ? 'rpx' : 'upx'}`, height: `${v.h}${nav ? 'rpx' : 'upx'}` }"
          v-for="(v, i) in buttons" :key="i">
          <image model="aspectFill" :src="v.path"></image>
        </view>
      </view>
    </block>
  </view>
</template>

<script>
export default {
  name: "lHeader",
  components: {},
  props: {
    title: {
      type: String,
      default: "",
    },
    color: {
      type: String,
      default: "#000000",
    },
    bgcolor: {
      type: String,
      default: "#ffffff",
    },
    noBack: {
      type: Boolean,
      default: false,
    },
    callback: {
      type: Boolean,
      default: false,
    },
    buttons: {
      type: Array,
      default: () => [],
    },
    back: {
      type: Object,
      default: () => ({}),
    },
  },
  data() {
    return {};
  },
  computed: {
    nav() {
      return uni.getStorageSync("navInfo");
    },
  },
  watch: {},
  created() { },
  onLoad(d) { },
  onShow() { },
  methods: {
    goBack() {
      if (this.callback) {
        this.$emit("back");
        return;
      }
      let pages = getCurrentPages();
      let beforePage = pages[pages.length - 2];
      this.$emit("back");

      if (this.back.path) {
        this.$util.linkTo(this.back.path, this.back.mode);
        return;
      }

      if (beforePage) {
        uni.navigateBack({
          delta: 1,
        });
      } else {
        this.$util.linkTo("/pages/tabbar/workbench", "switch");
      }
    },
    handler(v) {
      this.$emit("handler", v);
    },
  },
};
</script>
<style lang='less' scoped>
.header {
  z-index: 10;
  position: fixed;
  left: var(--window-left);
  right: var(--window-right);
  height: 44px;
  height: calc(44px + var(--status-bar-height));
  padding: 7px 3px;
  padding-top: calc(7px + var(--status-bar-height));
  display: -webkit-box;
  display: -webkit-flex;
  display: flex;
  overflow: hidden;
  -webkit-box-pack: justify;
  -webkit-justify-content: space-between;
  justify-content: space-between;
  -webkit-transition-property: all;
  transition-property: all;

  .uni-btn-icon {
    color: #000000;
    font-size: 54upx;
  }

  .back {
    .flex(row, center);
    width: 50upx;
    height: 100%;

    .back-icon {
      width: 16upx;
      height: 32upx;
    }
  }

  .title {
    position: absolute;
    top: var(--status-bar-height);
    bottom: 0;
    left: 70px;
    right: 70px;
    height: 44px;
    line-height: 44px;
    font-size: 36upx;
    font-weight: bold;
    color: #000000;
    text-align: center;
    overflow: hidden;
    white-space: nowrap;
    text-overflow: ellipsis;
  }
}

.list {
  .flex(row-reverse, center);
  width: fit-content;
  height: 100%;

  .single {
    position: relative;
    .flex(row, center);

    &:last-child {
      margin-left: 30upx;
    }

    &:not(:last-child) {
      margin-left: 60upx;

      &::before {
        content: '';
        position: absolute;
        top: 50%;
        left: -25upx;
        transform: translate(0, -50%);
        width: 2upx;
        height: 24upx;
        background-color: #E6E6E6;
      }
    }
  }
}

.inner {
  position: absolute;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;

  .back {
    .flex();
    width: 50upx;
    height: 50upx;

    .back-icon {
      margin-left: 30upx;
      width: 16upx;
      height: 32upx;
    }
  }

  .title {
    margin: 0 28upx 0 16upx;
    font-size: 32upx;
    font-weight: 500;
    color: #000000;
  }

  .all-title {
    position: absolute;
    top: 0;
    bottom: 0;
    left: 50upx;
    right: 50upx;
    margin: 0;
    text-align: center;
    font-size: 36upx;
    font-weight: bold;
    color: #000000;
    pointer-events: none;
  }
}
</style>